package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.model.BaseCategoryTrademark;
import com.atguigu.gmall.product.model.CategoryTrademarkVo;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 分类品牌中间表 业务实现类
 *
 * @author lan
 * @since 2023-04-18
 */
@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper, BaseCategoryTrademark> implements BaseCategoryTrademarkService {

    /**
     * 将品牌关联到分类
     *
     * @param categoryTrademarkVo
     * @return
     */
    @Override
    public void saveBaseCategoryTrademark(CategoryTrademarkVo categoryTrademarkVo) {
            //先获取品牌id集合
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();
        if (!CollectionUtils.isEmpty(trademarkIdList)){
            List<BaseCategoryTrademark> list = trademarkIdList.stream().map(id -> {
                //long转BaseCategoryTrademark型,,往BaseCategoryTrademark表里存
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setTrademarkId(id);
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
                return baseCategoryTrademark;
            }).collect(Collectors.toList());
            //存
            this.saveBatch(list);

        }
    }

    /**
     * 删除!!!!!!解除品牌分类关联
     *
     * @param category3Id
     * @param trademarkId
     * @return
     */
    @Override
    public void deleteCategoryTrademark(Long category3Id, Long trademarkId) {
        //逻辑删除
        LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id,category3Id);
        queryWrapper.eq(BaseCategoryTrademark::getTrademarkId,trademarkId);
        this.remove(queryWrapper);
        //物理删除,自定义
        baseMapper.deleteCategoryTrademark( category3Id,  trademarkId);
    }
}
